<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-7-19
 * Time: 上午10:41
 */

namespace Actions;

use Data\Retreat_order_form_DAO;
use Data\Order_form_DAO;
use Data\Order_for_foods_DAO;

class RestaurantRetreatOrderFormConfirm extends BaseAction
{
    public function doAction() {
        parent::doAction();
        // 退单id
        $retreat_form_id = (int)$this->data[RETREAT_FORM_ID];
        // 退单记录
        $retreat_form_record = Retreat_order_form_DAO::getRecord($retreat_form_id);
        if (!$retreat_form_record) {
            return $this->SendErrMsg("错误,没有该退单信息");
        }
        // 订单id
        $order_form_id = $retreat_form_record->getORDER_FORM_ID();
        // 订单记录
        $order_form_record = Order_form_DAO::getRecord($order_form_id);
        if (!$order_form_record) {
            return $this->SendErrMsg("错误，没有此订单信息");
        }
        // 是否已付款
        $order_form_pay_status = $order_form_record->getPAY_STATUS();
        if(!$order_form_pay_status) {
            return $this->SendErrMsg("订单未付款，不能退单");
        }
        // 商家id
        $restaurant_id = $order_form_record->getRESTAURANT_ID();
        if ($restaurant_id!=$_SESSION[RESTAURANT_ID]) {
            return $this->SendErrMsg("错误，不能退单");
        }

        // 退单操作
        // 订单信息改为退单
        $order_form_record->setPAY_STATUS(STATE_NO);

        // 订单内商品改为退单
        // 订单中商品信息
        $order_for_food_list = Order_for_foods_DAO::getListByOrderFormIdRecord($order_form_id);
        foreach ($order_for_food_list as $order_for_food_record) {
            $order_for_food_record->setPAY_STATUS(STATE_YES);
            Order_for_foods_DAO::update($order_for_food_record);
        }

        // 返回数据
        $this->ResponseDataToCurrentClient();
    }
}